package com.store.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.store.entity.EmployeePerformance;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 员工业绩Mapper
 */
@Mapper
public interface EmployeePerformanceMapper extends BaseMapper<EmployeePerformance> {
    
    /**
     * 统计员工总业绩
     */
    @Select("SELECT " +
            "COALESCE(SUM(order_count), 0) as totalOrders, " +
            "COALESCE(SUM(sales_amount), 0) as totalSales, " +
            "COALESCE(SUM(refund_amount), 0) as totalRefunds, " +
            "COALESCE(SUM(net_sales), 0) as totalNetSales " +
            "FROM employee_performance " +
            "WHERE employee_id = #{employeeId}")
    Map<String, Object> selectTotalPerformance(@Param("employeeId") Long employeeId);
    
    /**
     * 统计员工指定日期范围业绩
     */
    @Select("SELECT " +
            "COALESCE(SUM(order_count), 0) as totalOrders, " +
            "COALESCE(SUM(sales_amount), 0) as totalSales, " +
            "COALESCE(SUM(refund_amount), 0) as totalRefunds, " +
            "COALESCE(SUM(net_sales), 0) as totalNetSales " +
            "FROM employee_performance " +
            "WHERE employee_id = #{employeeId} " +
            "AND stat_date BETWEEN #{startDate} AND #{endDate}")
    Map<String, Object> selectPerformanceByDateRange(
            @Param("employeeId") Long employeeId,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate
    );
    
    /**
     * 获取员工业绩趋势
     */
    @Select("SELECT " +
            "stat_date as date, " +
            "order_count as orderCount, " +
            "sales_amount as salesAmount, " +
            "refund_amount as refundAmount, " +
            "net_sales as netSales " +
            "FROM employee_performance " +
            "WHERE employee_id = #{employeeId} " +
            "AND stat_date BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY stat_date ASC")
    List<Map<String, Object>> selectPerformanceTrend(
            @Param("employeeId") Long employeeId,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate
    );
}




